package com.sptpc.demo.support;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.springframework.boot.jackson.JsonComponent;

import java.io.IOException;

@JsonComponent
public class MoneyDeserializer extends StdDeserializer<Money> {

    protected MoneyDeserializer() {
        super(Money.class);
    }

    //    实现自己针对json值->Money类的规则自定义
    @Override
    public Money deserialize(JsonParser jsonParser,
                             DeserializationContext deserializationContext)
            throws IOException, JacksonException {
        String  value = jsonParser.getValueAsString();
        return Money.of(CurrencyUnit.of("CNY"),Double.parseDouble(value));
    }
}
